package com.sansi.pencilbook.service.impl;

import com.sansi.pencilbook.DO.Result;
import com.sansi.pencilbook.entity.User;
import com.sansi.pencilbook.mapper.UserMapper;
import com.sansi.pencilbook.service.UserServer;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;

/**
 * 功能说明: 用户服务
 *
 * @author lsy13
 */
@Service
public class UserServerImpl implements UserServer {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Result register(User user) {
        return addUser(user);
    }

    @Override
    public Result login(String account, String password) {
        Result result = getUserByAccount(account);
        User user = (User) result.getData();
        if (user != null) {
            return user.getUserPassword().equals(password) ? Result.ofSuccess("登录成功") : Result.ofFailed("密码错误");
        }
        return Result.ofFailed("账户名不存在");
    }

    @Override
    public Result getAllUsers() {
        try {
            List<User> list = userMapper.findAll();
            return list.size()>0 ? Result.ofSuccess(list.size(), list)
                    : Result.ofFailed("查询为空");
        } catch (SQLException throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("查询用户失败");
        }
    }

    @Override
    public Result getUserByAccount(String account) {
        try{
            User user = userMapper.findUserByAccount(account);
            return user!=null ? Result.ofSuccess("查询用户成功",1, user)
                    : Result.ofFailed("该用户不存在");
        }catch (SQLException e){
            System.out.println(e.getMessage());
            return Result.ofFailed("查询用户失败");
        }
    }

    @Override
    public Result addUser(User user) {
        try {
            return Result.ofSuccess("注册成功", 1, userMapper.saveUser(user));
        } catch (Exception e){
            Throwable cause = e.getCause();
            if (cause instanceof SQLIntegrityConstraintViolationException) {
                String errmsg = cause.getMessage();
                //根据约束名称定位是那个字段
                if(StringUtils.isNotBlank(errmsg)) {
                    return Result.ofFailed("账户名重复");
                }
            }
            //处理非唯一约束
            return Result.ofFailed("注册失败");
        }
    }

    @Override
    public Result modifyUser(User user) {
        try{
            return Result.ofSuccess("修改用户信息成功", userMapper.updateUser(user));
        }catch (Exception e) {
            System.out.println(e.getMessage());
            return Result.ofFailed("修改用户信息失败");
        }
    }

    @Override
    public Result deleteUser(User user) {
        try {
            return Result.ofSuccess(userMapper.deleteUser(user));
        } catch (SQLException throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("删除用户失败");
        }
    }

    @Override
    public Result deleteUsers(List<User> users) {
        try {
            return Result.ofSuccess("批量删除用户成功",userMapper.deleteUsers(users));
        } catch (SQLException throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("批量删除用户失败");
        }
    }
}
